package com.k7.mobile.platform.web.interceptor;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.StrutsStatics;

import com.k7.mobile.platform.bo.IUserBO;
import com.k7.mobile.platform.dataobject.UserDO;
import com.k7.mobile.platform.web.support.Constants;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.qike.java.pojo.User;

public class UserInterceptor extends AbstractInterceptor {

	private static final long serialVersionUID = 6301129967419590955L;

	private IUserBO userBO;
	public void setUserBO(IUserBO userBO) {
		this.userBO = userBO;
	}
	
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {	
		ActionContext actionContext = invocation.getInvocationContext();
		HttpServletRequest request = (HttpServletRequest) actionContext.get(StrutsStatics.HTTP_REQUEST);
		User u = userBO.isLogin(request);
		UserDO user = null;
		if (u != null) {
	
				// 缓存不存在用户，判断DB中是否存在
				user = userBO.getUserById(u.getUid().longValue());
				if (user == null) {
					user = new UserDO();
					user.setUserId(u.getUid().longValue());
					user.setUserNick(u.getNick());
					user.setAdminRole(Constants.USER_ADMIN_ROLE_NORMAL);
					user.setStatus(Constants.USER_STATUS_NORMAL);
					userBO.addUser(user);
				}
				
		}

		return invocation.invoke();
	}
}
